/*
 * Copyright 2017 Zhihu Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ztstech.matisse_android_plugin.matisse;

import android.content.Context;
import android.graphics.Point;

import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.IncapableCause;
import com.zhihu.matisse.internal.entity.Item;
import com.zhihu.matisse.internal.utils.PhotoMetadataUtils;

import java.util.HashSet;
import java.util.Set;


public class MediaFilter extends Filter {

    private static final int MAX_SIZE = 512 * 1024 * 1024;

    private static final int MAX_DURATION = 60 * 1000 * 5 + 1000;

    private int mMaxSize;

    /**
     *   高宽比
     */
    private float mScale;

    public MediaFilter(int maxSizeInBytes, float mScale) {
        this.mMaxSize = maxSizeInBytes;
        this.mScale = mScale;
    }

    @Override
    public Set<MimeType> constraintTypes() {
        return new HashSet<MimeType>() {{
            add(MimeType.GIF);
        }};
    }

    @Override
    public IncapableCause filter(Context context, Item item) {
        Point size = PhotoMetadataUtils.getBitmapBound(context.getContentResolver(), item.getContentUri());
        if (item.size == 0) {
            // 图片大小等于0
            return new IncapableCause(IncapableCause.DIALOG, "无效图片请重新选择");
        }
        if (item.isImage()) {
            if (item.size > mMaxSize) {
                return new IncapableCause(IncapableCause.DIALOG, "图片大小不能超过" + mMaxSize / 1024 / 1024 + "M");
            }

            if (size.x > 5000) {
                return new IncapableCause(IncapableCause.DIALOG, "图片宽度不能大于5000像素，请重新处理图片后再上传哦~");
            } else if (size.y > 5000) {
                return new IncapableCause(IncapableCause.DIALOG, "图片高度不能大于5000像素，请重新处理图片后再上传哦~");
            }
            if ((size.x * 1.0 / size.y) >= mScale) {
                return new IncapableCause(IncapableCause.DIALOG, "宽高比不能大于4");
            }
        }
        if (item.isVideo()) {
            if (item.duration < 1000) {
                // 视频时长小于1秒
                return new IncapableCause(IncapableCause.DIALOG, "视频时长过短，无法上传");
            }
//            if (item.size > MAX_SIZE) {
//                return new IncapableCause(IncapableCause.DIALOG, "视频大小不能超过500MB");
//            }
            if (item.duration > MAX_DURATION){
                return new IncapableCause(IncapableCause.DIALOG,"视频长度不能大于5分钟");
            }
        }
        return null;
    }

}
